package com.xiguthings.waka.web.controller.wechat.wx.beans;

import com.alibaba.fastjson.JSON;
import com.xiguthings.waka.web.controller.wechat.wx.beans.msgbody.*;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;

public class Msg {
	// openid
	private String touser;
	// 消息类型，文本为text，图片为image，语音为voice，视频消息为video，音乐消息为music，图文消息（点击跳转到外链）为news，图文消息（点击跳转到图文消息页面）为mpnews，卡券为wxcard
	private String msgtype;
	// 文本
	private TextBody text;
	// 图片
	private ImageBody image;
	// 语音
	private VoiceBody voice;
	// 视频
	private VideoBody video;
	// 音乐
	private MusicBody music;
	// 跳转外连接的图文消息
	private NewsBody news;
	// 发送图文消息（点击跳转到图文消息页面）
	private MpnewsBody mpnews;
	// 发送卡券
	private WxcardBody wxcard;

	public String getTouser() {
		return touser;
	}

	public void setTouser(String touser) {
		this.touser = touser;
	}

	public String getMsgtype() {
		return msgtype;
	}

	public void setMsgtype(String msgtype) {
		this.msgtype = msgtype;
	}

	public TextBody getText() {
		return text;
	}

	public void setText(TextBody text) {
		this.text = text;
	}

	public ImageBody getImage() {
		return image;
	}

	public void setImage(ImageBody image) {
		this.image = image;
	}

	public VoiceBody getVoice() {
		return voice;
	}

	public void setVoice(VoiceBody voice) {
		this.voice = voice;
	}

	public VideoBody getVideo() {
		return video;
	}

	public void setVideo(VideoBody video) {
		this.video = video;
	}

	public MusicBody getMusic() {
		return music;
	}

	public void setMusic(MusicBody music) {
		this.music = music;
	}

	public NewsBody getNews() {
		return news;
	}

	public void setNews(NewsBody news) {
		this.news = news;
	}

	public MpnewsBody getMpnews() {
		return mpnews;
	}

	public void setMpnews(MpnewsBody mpnews) {
		this.mpnews = mpnews;
	}

	public WxcardBody getWxcard() {
		return wxcard;
	}

	public void setWxcard(WxcardBody wxcard) {
		this.wxcard = wxcard;
	}

	public String toWxMsgJsonStr() {
		if (StringUtils.isNotBlank(touser) && (StringUtils.isNotBlank(msgtype))) {
			Class<? extends Msg> clazz = this.getClass();
			Field[] declaredFields = clazz.getDeclaredFields();
			for (Field field : declaredFields) {
				if (!field.getName().equals(msgtype) && !field.getName().equals(touser)) {
					field.setAccessible(true);
					try {
						field.set(this, null);
					} catch (IllegalArgumentException | IllegalAccessException e) {
						e.printStackTrace();
						throw new RuntimeException();
					}
				}
			}
			return JSON.toJSONString(this);
		}
		return null;

	}
}
